package com.ojoin.ojoin.eb_spider.service.catchdata;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;

import com.ojoin.ojoin.eb_spider.domain.BankCardCheck;
import com.ojoin.ojoin.eb_spider.service.catchdata.suning.SUNINGLogin;
import com.ojoin.ojoin.eb_spider.service.impl.SLDataCatchBaseServiceImpl;
import com.ojoin.trade.common.exception.BusinessException;

public abstract class SUNINGBaseDataCatchServiceImpl extends
		SLDataCatchBaseServiceImpl {

	@Autowired
	private SUNINGLogin login;
	
	@Override
	public boolean LoginByUserInfo(String baseUrl, String username,
			String password) throws Exception {

		int loginCount = 1;

		while (loginCount < verifycodeRetryCount) {
			loginCount++;

			login.init(this.getWebDriver(), username, password,this.getAssistCatchData().getStartMess().getPlatformUser().getId()+"",this.getAssistCatchData().getStartMess().getPlatformUser().getPlatformName());
 
			login.run();

			if(login.islogin()){
				return true ;
			}else {
				if (login.isVerifycodeError()) {
					continue;
				} else if (login.isUserAndPassword()) {
					throw new BusinessException("输入的账户名与密码不匹配");
				} else {
					throw new BusinessException("登陆失败");
				} 
			}
			
		}
 
		return login.islogin();
	}

	@Override
	public String catchSupplierName() throws BusinessException {

		return this.getWebDriver()
				.getText(
						"css",
						"#divNotContent > div.containC > div.singleInfo > div > div > div > dl:nth-child(1) > dd");

	}

	@Override
	public 	BankCardCheck catchSupplierAccount() throws BusinessException {
		String checkBankAccount = "http://sop.suning.com/sel/companyInfoManage/newShowCompanyDetail.action";
		this.getWebDriver().get(checkBankAccount);
		// 
		if(!this.getWebDriver().getDriver().getCurrentUrl().equals(checkBankAccount)){
			String password = getLoginPassword(this.getAssistCatchData().getStartMess().getPlatformUser()
					.getPassword());
			this.getWebDriver().sendKeys("id", "pwd", password);
			this.getWebDriver().click("css", "#pageImg > div > div.form-cont > p:nth-child(6) > a");
		}
		
		BankCardCheck bcc = new BankCardCheck();
		
		String bankNum = this.getWebDriver()
				.getText(
						"css",
						"body > div.main.clearfix > div.main-context > div > div > div > div > div:nth-child(4) > table > tbody > tr:nth-child(3) > td.qulifi-lali");
		
		String bankOfDeposit = this.getWebDriver()
				.getText(
						"css",
						"body > div.main.clearfix > div.main-context > div > div > div > div > div:nth-child(4) > table > tbody > tr:nth-child(2) > td.qulifi-lali");
		
		
		bcc.setBankNum(bankNum);
		bcc.setBankOfDeposit(bankOfDeposit);
		return bcc;
		

	}
	
	@Override
	public BankCardCheck isSupplyAccountCorrect() throws BusinessException {
		boolean checkflag = true;

		BankCardCheck bcc= catchSupplierAccount();
 
		bcc.setCorrect(checkflag);
		return bcc;
	}
	
	@Override
	public void initBusinessData() throws Exception {
		/*if(!isCacheLogin()){
        	if(login.islogin()){
        		login.run();
        	}
        	
        }*/
     
	}
	
	// TODO
	@Override
	public void saveCookies() throws IOException {
		
	}
	
}
